﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Registered_Student_Management
{
    public partial class F_NewClass : Form
    {
        string connection_string = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Database.mdb;Jet OLEDB:Database Password=51002201";
        public F_NewClass()
        {
            InitializeComponent();
        }

        public int center;
        public int getCenter(int aa)
        {
            center = aa;
            return center;
        }

        public int idCourse;
        public int getIDCourse(string a)
        {
            idCourse = int.Parse(a.ToString());
            return idCourse;
        }

        public string codeCourse;
        public string getCocode(string b)
        {
            codeCourse = b;
            return codeCourse;
        }

        public string nameCourse;
        public string getConame(string c)
        {
            nameCourse = c;
            return nameCourse;
        }
        private void F_NewClass_Load(object sender, EventArgs e)
        {
            GB_NewClass.Visible = true;
        }

        private void GB_NewClass_VisibleChanged(object sender, EventArgs e)
        {
            OleDbConnection cnData = new OleDbConnection();
            cnData.ConnectionString = connection_string;
            OleDbCommand commandData1 = new OleDbCommand();
            commandData1.Connection = cnData;
            commandData1.CommandText = "select id, fullname from teacher";
            OleDbDataAdapter adapt1 = new OleDbDataAdapter(commandData1);
            DataSet ds1 = new DataSet();
            adapt1.Fill(ds1);

            CbB_giangvienLopmoi.DataSource = ds1.Tables[0];
            CbB_giangvienLopmoi.DisplayMember = "fullname";
            CbB_giangvienLopmoi.ValueMember = "id";

            // load data to combobox sobuoihoc
            string[] s = { "S246", "C246", "T246", "S357", "C357", "T357" };

            this.CbB_buoiLopmoi.DataSource = s;
        }

        private void BT_moLopmoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_phonghocLopmoi.Text == "" || CbB_giangvienLopmoi.Text == "" || CbB_buoiLopmoi.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }

                else
                {
                    OleDbConnection cnInsert = new OleDbConnection();
                    cnInsert.ConnectionString = connection_string;

                    OleDbCommand commandInsertTest = new OleDbCommand();
                    commandInsertTest.Connection = cnInsert;
                    cnInsert.Open();
                    string room = TB_phonghocLopmoi.Text;
                    string buoihoc = CbB_buoiLopmoi.Text.ToString();
                    commandInsertTest.CommandText = "select * from class where [time] = '"+ buoihoc +"' and mid(ccode,7) ='"+ room +"' ";
                    OleDbDataAdapter adapt = new OleDbDataAdapter(commandInsertTest);
                    DataTable data = new DataTable();
                    adapt.Fill(data);

                    if (data.Rows.Count > 0)
                    {
                        MessageBox.Show("Lớp Vừa Nhập Hiện Đã Được Mở ", "Thông Báo");
                    }
                    else
                    {
                        OleDbCommand commandInsertNewClass = new OleDbCommand();
                        commandInsertNewClass.Connection = cnInsert;
                        int id_teacher = int.Parse(CbB_giangvienLopmoi.SelectedValue.ToString());
                        string name_teacher = CbB_giangvienLopmoi.Text.ToString();
                        int limit_student = int.Parse(TB_gioihanhvLopmoi.Text.ToString());
                        string X = "";
                        if (center == 1) X = "A_";
                        else if (center == 2) X = "K_";
                        else if (center == 3) X = "LT_";
                        string ccode = String.Concat(codeCourse,X, room);

                        //insert new class to database


                        commandInsertNewClass.CommandText = "insert into class (ccode, cname,student_number,limit_student,id_teacher, name_teacher, [time],belong) values('" + ccode + "','" + nameCourse + "', '" + 0 + "','" + limit_student + "','" + id_teacher + "','" + name_teacher + "','" + buoihoc + "','" + idCourse + "')";
                        commandInsertNewClass.ExecuteNonQuery();

                        cnInsert.Close();
                        cnInsert.Dispose();
                        cnInsert = null;

                        MessageBox.Show("Tạo Lớp Mới Thành Công", "Thông Báo");
                        Close();
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

    }
}
